package com.why.zing.payapi.service.impl;

import com.why.zing.payapi.constant.PayCodeEnum;
import com.why.zing.payapi.dao.PayOrderMapper;
import com.why.zing.payapi.entity.PayOrder;
import com.why.zing.payapi.exception.PayException;
import com.why.zing.payapi.service.PayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class PayOrderServiceImpl implements PayOrderService {

    @Autowired
    private PayOrderMapper payOrderMapper;

    /**
     * 更新支付单
     *
     * @param payOrder 支付单信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePayOrder(PayOrder payOrder) {
        payOrderMapper.updateByPrimaryKeySelective(payOrder);
    }

    /**
     * 新增支付单信息
     *
     * @param payOrder 支付单信息
     * @return 支付单信息（返回ID）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PayOrder insertSelective(PayOrder payOrder) {
        payOrderMapper.insertSelective(payOrder);
        return payOrder;
    }

    /**
     * 查询一个支付单信息
     *
     * @param payOrder 支付单信息（查询条件）
     * @return 支付单信息
     */
    @Override
    public PayOrder selectOne(PayOrder payOrder) {
        List<PayOrder> payOrderList = payOrderMapper.select(payOrder);
        if (CollectionUtils.isEmpty(payOrderList)) {
            throw new PayException(PayCodeEnum.ORDER_NOT_EXIST);
        }
        return payOrderList.get(0);
    }

    /**
     * 查询支付单列表
     *
     * @param payOrder 查询条件
     * @return 支付单列表
     */
    @Override
    public List<PayOrder> select(PayOrder payOrder) {
        return payOrderMapper.select(payOrder);
    }
}
